switch(variable) { case value: --------- --------- break; case value: --------- --------- break; . . default: --------- --------- }
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.println("Enter a single digit number :"); int a=stdin.nextInt(); switch(a) { case 0: System.out.println("Zero");break; case 1: System.out.println("One");break; case 2: System.out.println("Two");break; case 3: System.out.println("Three");break; case 4: System.out.println("Four");break; case 5: System.out.println("Five");break; case 6: System.out.println("Six");break; case 7: System.out.println("Seven");break; case 8: System.out.println("Eight");break; case 9: System.out.println("Nine");break; default:System.out.println("Invalid input"); } } }
Enter a single digit number 7 Seven
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.println("Enter Month in digit:"); int a=stdin.nextInt(); switch(a) { case 1: System.out.println("Jan");break; case 2: System.out.println("Feb");break; case 3: System.out.println("Mar");break; case 4: System.out.println("Apr");break; case 5: System.out.println("May");break; case 6: System.out.println("Jun");break; case 7: System.out.println("Jul");break; case 8: System.out.println("Aug");break; case 9: System.out.println("Sep");break; case 10: System.out.println("Oct");break; case 11: System.out.println("Nov");break; case 12: System.out.println("Dec");break; default:System.out.println("Invalid input"); } } }
Enter month in digit 3 Mar
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.println("Enter color code: "); String a = stdin.next(); switch( a ) { case "R": case "r":System.out.println("Red");break; case "G": case "g":System.out.println("Green");break; case "B": case "b":System.out.println("Blue");break; default : System.out.println("White"); } } }
Enter Color Code g Green
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.println("Enter simple exp:"); float a=stdin.nextFloat(); String op=stdin.next(); float b=stdin.nextFloat(); switch(op) { case "+": System.out.println("="+(a+b));break; case "-": System.out.println("="+(a-b));break; case "x": System.out.println("="+(a*b));break; case "/": System.out.println("="+(a/b));break; default: System.out.println("Invalid operator"); } } }
Enter simple Exp: 2.4 + 7.1 = 11.5 Enter simple exp: 5 x 12 = 60
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.println("Enter temp in range 0-99:"); int a=stdin.nextInt(); switch(a/20) { case 0: System.out.println("cold");break; case 1: System.out.println("normal");break; case 2: System.out.println("warm");break; case 3: System.out.println("hot");break; case 4: System.out.println("very hot");break; default : System.out.println("invalid input"); } } }
Enter simple Exp: 2.4 + 7.1 = 11.5